home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume7 / mailwatcher < prev    next >
Encoding:
Text File  |  1989-07-21  |  6.4 KB  |  238 lines

  1. Newsgroups: comp.sources.misc
  2. subject: v07i090: A Simple Mailwatcher
  3. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  4. Reply-To: sam@lfcs.ed.ac.uk ("S. Manoharan")
  5.  
  6. Posting-number: Volume 7, Issue 90
  7. Submitted-by: sam@lfcs.ed.ac.uk ("S. Manoharan")
  8. Archive-name: mailwatcher
  9.  
  10. [[A csh script?  One would think that the author's system would have "biff".
  11.   ++bsa]]
  12.  
  13. This is a mailwatcher that keeps checking the mbox for the
  14. arrival of new mails. There is a shell script ( slower )
  15. and a C source. 
  16.  
  17. S. Manoharan ( sam@lfcs.edinburgh.ac.uk )
  18.  
  19. #! /bin/sh
  20. # This is a shell archive.  Remove anything before this line, then unpack
  21. # it by saving it into a file and typing "sh file".  To overwrite existing
  22. # files, type "sh file -c".  You can also feed this as standard input via
  23. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  24. # will see the following message at the end:
  25. #        "End of shell archive."
  26. # Contents:  mailwatcher.csh mailwatcher.c makefile
  27. # Wrapped by sam@cheops on Wed Jul 12 14:06:09 1989
  28. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  29. if test -f mailwatcher.csh -a "${1}" != "-c" ; then 
  30.   echo shar: Will not over-write existing file \"mailwatcher.csh\"
  31. else
  32. echo shar: Extracting \"mailwatcher.csh\" \(405 characters\)
  33. sed "s/^X//" >mailwatcher.csh <<'END_OF_mailwatcher.csh'
  34. X#
  35. X# mailwatcher (c) s. manoharan edinburgh u
  36. X#             sam@lfcs.edinburgh.ac.uk
  37. X# 
  38. X
  39. Xonintr - ;
  40. Xset username = `whoami`;
  41. Xset oldmailsize = 0;
  42. Xset alert = "";
  43. X
  44. Xwhile ( 1 )
  45. X   set newmailsize = `wc -c < /usr/spool/mail/$username`;
  46. X
  47. X   if ( $newmailsize > $oldmailsize ) then
  48. X      echo $alert;
  49. X      echo "You have new mail";
  50. X      from;
  51. X   endif 
  52. X   set oldmailsize = $newmailsize;
  53. X   sleep 60;
  54. Xend
  55. X
  56. END_OF_mailwatcher.csh
  57. echo shar: 3 control characters may be missing from \"mailwatcher.csh\"
  58. if test 405 -ne `wc -c <mailwatcher.csh`; then
  59.     echo shar: \"mailwatcher.csh\" unpacked with wrong size!
  60. fi
  61. chmod +x mailwatcher.csh
  62. # end of overwriting check
  63. fi
  64. if test -f mailwatcher.c -a "${1}" != "-c" ; then 
  65.   echo shar: Will not over-write existing file \"mailwatcher.c\"
  66. else
  67. echo shar: Extracting \"mailwatcher.c\" \(3210 characters\)
  68. sed "s/^X//" >mailwatcher.c <<'END_OF_mailwatcher.c'
  69. X/* mailwatcher.c -- author: sam@lfcs.edinburgh.ac.uk */
  70. X
  71. X#ifndef lint
  72. Xstatic char *scid = "s. manoharan edinburgh u";
  73. X#endif
  74. X
  75. X#define VERSION        1
  76. X#define EDITION        0
  77. X#define PATCH        0
  78. X
  79. X#include <stdio.h>
  80. X
  81. X#define UNSIZE    8
  82. X#define SLEEP    60
  83. X#define SPOOL    "/usr/spool/mail/"
  84. X#define ALERT    ""
  85. X
  86. X
  87. Xtypedef enum { false = 0, true = 1 } Boolean;
  88. X
  89. X
  90. Xvoid main(argc,argv)
  91. Xint argc; char *argv[];
  92. X{
  93. X   int opch; extern int Optind; extern char *Optarg;
  94. X   char *mbox; unsigned seconds = SLEEP; FILE *fp;
  95. X   int oldsize = 0, newsize;
  96. X   char *getenv(), *malloc(), *strcpy(), *strcat();
  97. X   void Usage();
  98. X
  99. X   while ( ( opch = Getopt(argc,argv,"Vn:") ) != -1 )
  100. X      switch ( opch ) {
  101. X      case 'V'   :
  102. X     (void)fprintf(stderr,"%s version %d.%d.%d  ",argv[0],
  103. X        VERSION, EDITION, PATCH);
  104. X     (void)fprintf(stderr,"(c) s. manoharan  edinburgh univ\n");
  105. X     exit(0);
  106. X     break;
  107. X      case 'n'    :
  108. X     if ( ( seconds = (unsigned)atoi(Optarg) ) == 0 )
  109. X        seconds = SLEEP;
  110. X     break;
  111. X      default    :
  112. X     Usage(argv[0]);
  113. X     exit(0);
  114. X      } /* ensw */
  115. X
  116. X   if ( ( mbox = malloc(sizeof(SPOOL)+UNSIZE) ) != (char *)0 ) {
  117. X      (void)strcpy(mbox,SPOOL);
  118. X      (void)strcat(mbox,getenv("USER"));
  119. X   }
  120. X   else {
  121. X      (void)fprintf(stderr,"%s: cannot get space\n",argv[0]);
  122. X      exit(0);
  123. X   }
  124. X
  125. X   for ( ; ; ) {
  126. X
  127. X      /* check size of SPOOL/USER */
  128. X      if ( (fp = fopen(mbox,"r")) != (FILE *)0 ) {
  129. X     newsize = 0;
  130. X     while ( getc(fp) != EOF )
  131. X        ++newsize;
  132. X     if ( newsize > oldsize )
  133. X        (void)printf("%s\nYou have new mail\n",ALERT);
  134. X     oldsize = newsize;
  135. X      }
  136. X      else {
  137. X     (void)fprintf(stderr,"%s: cannot open %s\n",argv[0],mbox);
  138. X     exit(0);
  139. X      }
  140. X      (void)fclose(fp);
  141. X      sleep(seconds); /* sleep for a while */
  142. X   } /* enfo */
  143. X
  144. X} /* enma */
  145. X
  146. Xvoid Usage(progname)
  147. Xchar *progname;
  148. X{
  149. X
  150. X   (void)fprintf(stderr,"usage: %s [-V] [-n seconds]\n",progname);
  151. X   (void)fprintf(stderr,"\t-V\t\t: print version and exit\n");
  152. X   (void)fprintf(stderr,"\t-n t\t\t: lie dormant for `t' seconds\n");
  153. X} /* enUsage */
  154. X
  155. X/* ------------------------------------------------------ */
  156. X
  157. X
  158. X
  159. Xchar *Optarg; int Optind;
  160. X
  161. Xint Getopt(argc,argv,options)
  162. Xint argc; char **argv; char *options;
  163. X{
  164. X   char *str, *ptr; char opch; char *Strchr();
  165. X   static int flag = 0; static int Argc; static char **Argv;
  166. X
  167. X   if (  flag == 0 ) {
  168. X      Argc = argc; Argv = argv; flag = 1; Optind = 1;
  169. X   }
  170. X
  171. X   if ( Argc <= 1 ) return -1;
  172. X
  173. X   if ( --Argc >= 1 ) {
  174. X      str = *++Argv;
  175. X      if (*str != '-') return -1; /* argument is not an option   */
  176. X      else {  /* argument is an option */
  177. X     if ( ( ptr = Strchr(options, opch = *++str) ) != (char *) 0 ) {
  178. X        ++Optind;
  179. X            Optarg = ++str; /* point to rest of argument if any  */
  180. X            if ( ( *++ptr == ':' ) && ( *Optarg == '\0' ) ) {
  181. X               if (--Argc <= 0) return '?';
  182. X               Optarg = *++Argv; ++Optind;
  183. X            }
  184. X        return opch;
  185. X         }
  186. X     else if ( opch == '-' ) { /* end of options */
  187. X        ++Optind;
  188. X        return -1;
  189. X     }
  190. X         else return '?';
  191. X      }
  192. X   }
  193. X   return 0; /* this will never be reached */
  194. X
  195. X} /* EnGetopt */
  196. X
  197. Xchar *Strchr(s,c)
  198. Xchar *s; char c;
  199. X{
  200. X   while ( *s != '\0' ) {
  201. X      if ( *s == c ) return s;
  202. X      else ++s;
  203. X   }
  204. X   return ( (char *) 0 );
  205. X} /* EnStrchr */
  206. X
  207. END_OF_mailwatcher.c
  208. echo shar: 3 control characters may be missing from \"mailwatcher.c\"
  209. if test 3210 -ne `wc -c <mailwatcher.c`; then
  210.     echo shar: \"mailwatcher.c\" unpacked with wrong size!
  211. fi
  212. # end of overwriting check
  213. fi
  214. if test -f makefile -a "${1}" != "-c" ; then 
  215.   echo shar: Will not over-write existing file \"makefile\"
  216. else
  217. echo shar: Extracting \"makefile\" \(70 characters\)
  218. sed "s/^X//" >makefile <<'END_OF_makefile'
  219. XCFLAGS= -O
  220. Xinstall: mw
  221. X
  222. Xmw: mailwatcher.o
  223. X    cc -s -o mw mailwatcher.o
  224. X
  225. END_OF_makefile
  226. if test 70 -ne `wc -c <makefile`; then
  227.     echo shar: \"makefile\" unpacked with wrong size!
  228. fi
  229. # end of overwriting check
  230. fi
  231. echo shar: End of shell archive.
  232. exit 0
  233. Janet: sam@uk.ac.ed.lfcs                      S. Manoharan
  234. Uucp : ..!mcvax!ukc!lfcs!sam                  Dept of Computer Science
  235. Arpa : sam%lfcs.ed.ac.uk@nsfnet-relay.ac.uk   University of Edinburgh
  236. Voice: 031-667 5076 (home)                    Edinburgh EH9 3JZ    UK.
  237.  
  238.